home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6128 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.1 KB  |  63 lines

  1. Path: solon.com!not-for-mail
  2. From: seebs@solon.com (Peter Seebach)
  3. Newsgroups: comp.lang.c,comp.lang.c.moderated
  4. Subject: Re: File Based linked list
  5. Date: 22 Feb 1996 14:43:36 -0600
  6. Organization: Usenet Fact Police (Undercover)
  7. Sender: clc@solutions.solon.com
  8. Approved: clc@solutions.solon.com
  9. Message-ID: <4gikho$jbc@solutions.solon.com>
  10. References: <Pine.SOL.3.90.960219171637.21117B-100000@eddie>
  11. NNTP-Posting-Host: solutions.solon.com
  12.  
  13. In article <Pine.SOL.3.90.960219171637.21117B-100000@eddie>,
  14. Khan Riaz Ahmad  <a228khao@cdf.toronto.edu> wrote:
  15. >    Anyone know how to write a file based linked list... How 
  16. >different is it to implement then a regular memory based linked list?? 
  17. >Any source code would be greatly appriciated... BTW I am a student and I 
  18. >need it for an assignment...
  19.  
  20. This should work:
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. typedef struct { FILE *f; char *s; } l;
  25.  
  26. int
  27. main() {
  28.         char s[80], t[80] = "";
  29.         l l = { 0, 0 };
  30.  
  31.         while (fgets(s, 80, stdin)) {
  32.                 strtok(s, "\n");
  33.                 if (!strlen(s)) continue;
  34.                 if (strcmp(s, "quit") == 0) break;
  35.                 if (!*t) strcpy(t, s);
  36.                 if (l.f) { l.s = s; fprintf(l.f, "%s\n", l.s); fclose(l.f); }
  37.                 l.f = fopen(s, "w");
  38.                 if (!l.f) { break; }
  39.         }
  40.         fprintf(l.f, "\n") && fclose(l.f);
  41.         /* print the list out */
  42.         for (l.f = fopen(strcpy(l.s, t), "r"); l.f;
  43.                 (l.f = fopen(strtok(fgets(l.s, 80, l.f), "\n"), "r")) &&
  44.                         !remove(l.s)) {
  45.                 printf("%s\n", l.s);
  46.         }
  47.         printf("%s", l.s);
  48.         return 0;
  49. }
  50.  
  51. ---
  52. not as well tested as I'd like, but I'm in a bit of a hurry because
  53. I have to be at a meeting in a bit.  You should get the idea anyway.
  54.  
  55. Good luck!
  56.  
  57. -s
  58. -- 
  59. Peter Seebach - seebs@solon.com - Copyright 1995 Peter Seebach.
  60. C/Unix wizard -- C/Unix questions? Send mail for help.  No, really!
  61. FUCK the communications decency act.  Goddamned government.  [literally.]
  62. The *other* C FAQ - http://www.solon.com/~seebs/c/c-iaq.txt
  63.